

package ca.uwindsor;

import android.content.ContentValues;
import android.content.Context;
import android.database.*;
import android.database.sqlite.*;
import android.database.sqlite.SQLiteDatabase.CursorFactory;
import android.util.Log;


public class MyDBAdapter {

	 private static final String DATABASE_NAME = "myDatabase.db";
	  private String DATABASE_TABLE;
	  private String DATABASE_CREATE;
	  
	public void setDATABASE_CREATE(String dATABASE_CREATE) {
		DATABASE_CREATE = dATABASE_CREATE;
		
	}

	public String getDATABASE_TABLE() {
		return DATABASE_TABLE;
	}

	public void setDATABASE_TABLE(String dATABASE_TABLE) {
		DATABASE_TABLE = dATABASE_TABLE;
	}
	

	private static final int DATABASE_VERSION = 1 ;
	 
	 /*// The index (key) column name for use in where clauses.
	  final String KEY_ID="_id";

	  // The name and column index of each column in your database.
	  final String KEY_NAME="Name"; 
	  final int NAME_COLUMN = 1;
	  final String KEY_SDATE="StartDate"; 
	  final String KEY_EDATE="EndDate"; 
	  final String KEY_COMMENTS="Comments"; 
	  // TODO: Create public field for each column in your table.
	  */
	
	// SQL Statement to create a new database.

	

	  // Variable to hold the database instance
	  private SQLiteDatabase db;
	  // Context of the application using the database.
	  private final Context context;
	  // Database open/upgrade helper
	  private myDbHelper dbHelper;

	
	  
	  public MyDBAdapter(Context _context) {
	    context = _context;
	    dbHelper = new myDbHelper(context, DATABASE_NAME, null, DATABASE_VERSION);
	  }
	  
	 

	  public MyDBAdapter open() throws SQLException {
	    db = dbHelper.getWritableDatabase();
	    return this;
	  }

	  public void close() {
	      db.close();
	  }

	  public int insertEntry(ContentValues newValues) {
	    // TODO: Create a new ContentValues to represent my row
	    // and insert it into the database.
		  
		  //ContentValues newValues = new ContentValues();
		 // newValues.put(key, _myObject.toString());
		  
		  int index = (int) db.insert(DATABASE_TABLE, null, newValues);
		  
	    return index;
	  }

	  public boolean removeEntry(Object KEY_ID,Object _rowIndex) {
	    return db.delete(DATABASE_TABLE, KEY_ID + "=" + _rowIndex, null) > 0;
	 
	  }

	  public Cursor getAllEntries () {
	    return db.query(DATABASE_TABLE, null, 
	                    null, null, null, null, null);
	  }

	  public Cursor getEntry(Object key, Object value) {
	    // TODO: Return a cursor to a row from the database and
	    // use the values to populate an instance of MyObject
		  
		  String where = key.toString() + "= '" + value.toString()+"'";
		  
		  Cursor result = db.query(DATABASE_TABLE, null, where, null, null,null,null);
		  
	    return result;
	  }
	  
	  //Use for more than one condition
	  public Cursor getEntry(String where) {
		    // TODO: Return a cursor to a row from the database and
		    // use the values to populate an instance of MyObject
			  
			  //String where = key.toString() + "= '" + value.toString()+"'";
			  
			  Cursor result = db.query(DATABASE_TABLE, null, where, null, null,null,null);
			  
		    return result;
		  }

	  public boolean updateEntry(Object key, Object value, ContentValues newValues) {
	    // TODO: Create a new ContentValues based on the new object
	    // and use it to update a row in the database.
		  
		  String where = key.toString() + "=" + value.toString();
		  
		  db.update(DATABASE_TABLE, newValues, where, null);
		  
	    return true;
	  }

	  public void executeSQL(String sqlString){
		  db.execSQL(sqlString);
	  }
	  
	  public Cursor getDistinct(String[] columnName){
		  
		  Cursor ret = db.query(true, DATABASE_TABLE, columnName, null, null, null, null, null, null);
		  
		return ret;
	  }
	  
	  
	  private class myDbHelper extends SQLiteOpenHelper {

	    public myDbHelper(Context context, String name, 
	                      CursorFactory factory, int version) {
	      super(context, name, factory, version);
	    }

	    // Called when no database exists in disk and the helper class needs
	    // to create a new one. 
	    @Override
	    public void onCreate(SQLiteDatabase _db) {
	      _db.execSQL(DATABASE_CREATE);
	    }

	    // Called when there is a database version mismatch meaning that the version
	    // of the database on disk needs to be upgraded to the current version.
	    @Override
	    public void onUpgrade(SQLiteDatabase _db, int _oldVersion, int _newVersion) {
	      // Log the version upgrade.
	      Log.w("TaskDBAdapter", "Upgrading from version " + 
	                             _oldVersion + " to " +
	                             _newVersion + ", which will destroy all old data");
	        
	      // Upgrade the existing database to conform to the new version. Multiple 
	      // previous versions can be handled by comparing _oldVersion and _newVersion
	      // values.

	      // The simplest case is to drop the old table and create a new one.
	      _db.execSQL("DROP TABLE IF EXISTS " + DATABASE_TABLE);
	      // Create a new one.
	      onCreate(_db);
	    }
	  }
	
}
